﻿using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using System.Text.RegularExpressions;

/// <summary>
/// String 类扩展
/// </summary>
public static partial class StringExtension
{

    /// <summary>
    /// 字符串是否为 null 或者 empty
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static bool IsNullOrEmpty(this string str)
    {
        return string.IsNullOrEmpty(str);
    }

    /// <summary>
    /// 转换成多语言
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string FormatLocalize(this string str)
    {
        var formatStr = (str);
        return formatStr.Replace("\\n", "\n");
        //return GameEntry.Localization.GetString(str).Replace("\\n", "\n");
    }

    public static string FormatLocalize(this string str, params object[] args)
    {
        var formatStr = (str);
        if (formatStr.IsNullOrEmpty())
        {
            //Log.Debug("不存在 key 为 {0} 的多语言文本", str);
            return str.Format(args);
        }
        return formatStr.Replace("\\n", "\n").Format(args);
        //return GameEntry.Localization.GetString(str).Replace("\\n", "\n")
        //    .Format(args);
    }

    /// <summary>
    /// 获取格式化字符串
    /// </summary>
    /// <param name="str">字符串格式</param>
    /// <param name="arg0">字符串参数 0</param>
    /// <returns>格式化后的字符串</returns>
    public static string Format(this string str, object arg0)
    {
        return Utility.Text.Format(str, arg0);
    }

    /// <summary>
    /// 获取格式化字符串
    /// </summary>
    /// <param name="str">字符串格式</param>
    /// <param name="arg0">字符串参数 0</param>
    /// <param name="arg1">字符串参数 1</param>
    /// <returns>格式化后的字符串。</returns>
    public static string Format(this string str, object arg0, object arg1)
    {
        return Utility.Text.Format(str, arg0, arg1);
    }

    /// <summary>
    /// 获取格式化字符串
    /// </summary>
    /// <param name="str">字符串格式</param>
    /// <param name="arg0">字符串参数 0</param>
    /// <param name="arg1">字符串参数 1</param>
    /// <param name="arg2">字符串参数 2</param>
    /// <returns>格式化后的字符串。</returns>
    public static string Format(this string str, object arg0, object arg1, object arg2)
    {
        return Utility.Text.Format(str, arg0, arg1, arg2);
    }

    /// <summary>
    /// 获取格式化字符串
    /// </summary>
    /// <param name="str">字符串格式</param>
    /// <param name="args">字符串参数</param>
    /// <returns>格式化后的字符串</returns>
    public static string Format(this string str, params object[] args)
    {
        try
        {
            return Utility.Text.Format(str, args);
        }
        catch (Exception e)
        {
            Debug.LogError($"字符串解析错误：{str}  {args} \n {e}");
            return str;
        }
    }

    /// <summary>
    /// 格式化文本大小及颜色
    /// </summary>
    /// <param name="str">文本内容</param>
    /// <param name="size">文本大小</param>
    /// <param name="color">文本颜色</param>
    /// <returns></returns>
    public static string RichText(this string str, int size, string color)
    {
        return Utility.RichText.Formt(str, size, color);
    }

    /// <summary>
    /// 格式化文本大小
    /// </summary>
    /// <param name="str">文本内容</param>
    /// <param name="size">文本大小</param>
    /// <returns></returns>
    public static string RichTextSize(this string str, int size)
    {
        return Utility.RichText.FormatSize(str, size);
    }

    public static string RichTextErrorColor(this string str)
        => str.RichTextColor(Constant.Color.PINK);

    public static string RichTextGreenColor(this string str)
        => str.RichTextColor(Constant.Color.GREEN);

    public static string RichTextYellowColor(this string str)
        => str.RichTextColor(Constant.Color.TEXT_YELLOW);

    /// <summary>
    /// 格式化文本颜色
    /// </summary>
    /// <param name="str">文本内容</param>
    /// <param name="size">文本颜色</param>
    /// <returns></returns>
    public static string RichTextColor(this string str, string color)
    {
        if (!color.Contains("#"))
        {
            color = "#" + color;
        }
        return Utility.RichText.FormatColor(str, color);
    }

    public static List<int> ToIntList(this string str)
    {
        List<int> list = new List<int>();
        if (!str.IsNullOrEmpty())
        {
            var splits = str.GameSplit(Enums.Split.Mode.Param);
            for (int i = 0; i < splits.Length(); i++)
            {
                var split = splits[i];
                list.Add(int.Parse(split));
            }
        }
        return list;
    }

    /// <summary>
    /// 字符串转枚举
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="str"></param>
    /// <returns></returns>
    public static T ToEnum<T>(this string str)
    {
        return Utility.Enum.ToEnum<T>(str);
    }

    /// <summary>
    /// 字符串转枚举
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="str"></param>
    /// <param name="result"></param>
    public static void TryToEnum<T>(this string str, out object result)
    {
        Utility.Enum.TryToEnum<T>(str, out result);
    }



    #region 界面相关
    /// <summary>
    /// 以字符索引开始匹配  把其中某些字符，按照富文本字体修改
    /// </summary>
    /// <param name="str"></param>
    /// <param name="format"></param>
    /// <param name="startIndex"></param>
    /// <returns></returns>
    public static string StringFormatAttr(this string str, string format, int startIndex)
    {
        int len = str.Length;
        if (startIndex < len)
        {
            string strForward = str.Substring(0, startIndex);
            string strBehind = str.Substring(startIndex, len - startIndex);

            str = format.FormatLocalize(strForward, strBehind);
        }
        return str;
    }

    /// <summary>
    /// 以正则匹配 把其中某些字符，按照富文本字体修改
    /// </summary>
    /// <param name="str"></param>
    /// <param name="format"></param>
    /// <param name="startIndex"></param>
    public static string StringFormatAttrRexDot(this string str, string format)
    {
        Regex r = new Regex("\\.(\\d{2})%$");
        bool ismatch = r.IsMatch(str);
        if (ismatch)
        {
            string[] arrs = r.Split(str);

            string strForward = arrs[0];
            string strBehind = arrs[1];

            str = format.FormatLocalize(strForward, strBehind);
        }
        return str;
    }


    /// <summary>
    /// 切割替换字符串
    /// </summary>
    /// <param name="str"></param>
    /// <param name="maxCount"></param>
    /// <param name="replaceStr"></param>
    /// <returns></returns>
    public static string StringSubString(this string str, int maxCount, string replaceStr = "...")
    {
        if (str.Length > maxCount)
        {
            str = str.Substring(0, maxCount - 1) + replaceStr;
        }

        return str;
    }
    #endregion


    /// <summary>
    /// 连接路径
    /// </summary>
    /// <param name="path1">路径1</param>
    /// <param name="path2">路径2</param>
    /// <returns></returns>
    public static string Combine(this string path, params string[] paths)
    {
        int capcity = path.Length;
        for (int i = 0; i < paths.Length; ++i)
        {
            paths[i] = paths[i].Replace("\\", "/");
            capcity += paths[i].Length;
        }
        capcity = capcity + paths.Length + 1;
        StringBuilder sb = new StringBuilder(capcity);
        sb.Append(path);
        for (int i = 0; i < paths.Length; ++i)
        {
            sb.Append("/");
            sb.Append(paths[i]);
        }
        return sb.ToString();
    }

}

